package com.zc.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zc.mall.dao.ProductMapper;
import com.zc.mall.pojo.Product;
import com.zc.mall.service.ICategoryService;
import com.zc.mall.service.IProductService;
import com.zc.mall.vo.ProductDetailVo;
import com.zc.mall.vo.ProductVo;
import com.zc.mall.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.zc.mall.enums.ProductStatusEnum.DELETE;
import static com.zc.mall.enums.ProductStatusEnum.OFF_SALE;
import static com.zc.mall.enums.ResponseEnum.PRODUCT_OFF_SALE_OR_DELETE;

/**
 * @author 曾川
 * @version 1.0.0
 * @ClassName ProductServiceImpl.java
 * @Description 商品实现类
 * @createTime 2021年06月07日 09:35:00
 */
@Service
@Slf4j
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ProductMapper productMapper;


    /**
     * 商品列表
     *
     * @param categoryId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseVo<PageInfo> list(Integer categoryId, Integer pageNum, Integer pageSize) {

        Set<Integer> categoryIdSet = new HashSet<>();
        if (categoryId != null) {
            //这里查询的是传入categoryId的子类目
            categoryService.findSubCategoryId(categoryId,categoryIdSet);
            //将传入的categoryId包含进来
            categoryIdSet.add(categoryId);
        }

        //常规
//        List<ProductVo> productVoList = new ArrayList<>();
//        List<Product> products = productMapper.selectByCategoryIdSet(categoryIdSet);
//        log.info(" prodcuts Set = {}",products);
//        for(Product product:products) {
//            ProductVo productVo = new ProductVo();
//            BeanUtils.copyProperties(product, productVo);
//            productVoList.add(productVo);
//        }

        //只需加入该行，就可以完成分页
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.selectByCategoryIdSet(categoryIdSet);
        //lambd+stream
        List<ProductVo> productVoList = productList.stream()
                .map(e -> {
                    ProductVo productVo = new ProductVo();
                    BeanUtils.copyProperties(e, productVo);
                    return productVo;
                })
                .collect(Collectors.toList());
        //返回的json
        PageInfo pageInfo = new PageInfo<>(productList);
        pageInfo.setList(productVoList);

        return ResponseVo.success(pageInfo);
    }

    /**
     * 商品详情
     *
     * @param productId
     * @return
     */
    @Override
    public ResponseVo<ProductDetailVo> deatil(Integer productId) {

        Product product = productMapper.selectByPrimaryKey(productId);

        //只对确定性条件判断
        if (product.getStatus().equals(OFF_SALE.getCode())
                || product.getStatus().equals(DELETE.getCode())) {
            return ResponseVo.error(PRODUCT_OFF_SALE_OR_DELETE);
        }

        ProductDetailVo productDetailVo = new ProductDetailVo();
        BeanUtils.copyProperties(product,productDetailVo);

        return ResponseVo.success(productDetailVo);
    }
}
